package cn.kgc.controller;

import cn.kgc.pojo.Role;
import cn.kgc.pojo.User;
import cn.kgc.service.UserService;
import cn.kgc.util.CodeUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.Servlet;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/*Controller层注解   一定要写*/
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping(value = "/login.do")
    public String login(@RequestParam(required = false) String userCode,
                        @RequestParam(required = false) String password,
                        HttpSession session,
                        Model model,
                        @RequestParam(required = false)String code){
        String verifCode=(String)session.getAttribute("code");
        if(code!=null && !code.equals("") && verifCode.equals(code)){
            User user=userService.login(userCode, password);
            if(user!=null){
                session.setAttribute("user",user);
                //查看该用户所有用的权限
                return "index";
            }else{
                model.addAttribute("error","用户名或密码不正确");
                return "login";
            }
        }else{
            model.addAttribute("error","验证码输入错误");
            return "login";
        }
    }

    //获取验证码
    @RequestMapping(value = "/getCode")
    public void getCode(HttpServletResponse response,HttpSession session){
        ByteArrayOutputStream output=new ByteArrayOutputStream();//创建字节输出流
        String code= CodeUtil.drawImage(output);//获取绘制的字符个数并放到字节输出流中
        System.out.println("验证码："+code);
        session.setAttribute("code",code);
        try {
            //获取响应输出流
            ServletOutputStream outputStream=response.getOutputStream();
            //将字节流写到响应流中返回给页面
            output.writeTo(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/queryRole")
    @ResponseBody
    public String queryRole(HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        List<Role> list=userService.queryRole(user.getRoleId());
        return JSON.toJSONString(list);
    }

    /*@RequestMapping(value = "/queryUser")
    public String queryAllUser(String userName, String userType, @RequestParam(defaultValue = "0") Integer pageIndex, HttpServletRequest request){

        return "jsp/user/user";
    }*/

    @RequestMapping(value = "/queryUser")
    @ResponseBody
    public String queryAllsub(@RequestParam(required = false,defaultValue = "1") Integer pageNum,String userName,String userType){
        System.out.println(userName);
        System.out.println(userType);
        //设置起始页和页大小
        PageHelper.startPage(pageNum,4);
        List<User> list=userService.queryAllUser(userName,userType);
        //创建页面信息 对原来的页面进行分页
        PageInfo<User> pageInfo=new PageInfo<User>(list);
        return JSON.toJSONString(pageInfo);
    }
    @RequestMapping(value = "/addUser")
    public String addUser(User user,HttpServletRequest request){
        user.setEntryTime(new Date());
        user.setState(1);
        Integer integer=userService.addUser(user);
        if(integer>0){
            return "jsp/user/user";
        }else{
            return "jsp/user/useradd";
        }
    }
    @RequestMapping(value = "/queryByIdUser/{uid}")
    public String queryByIdUser(@PathVariable String uid, Model model){
        User user=userService.queryByIdUser(Integer.parseInt(uid));
        model.addAttribute("userById",user);
        if(user!=null){
            return "jsp/user/updateUser";
        }else{
            return "jsp/user/user";
        }
    }

    @RequestMapping(value = "/updateUser")
    public String updateUser(User user){
        user.setUpdateTime(new Date());
        Integer i=userService.upDateUser(user);
        if (i>0){
            return "jsp/user/user";
        }else{
            return "jsp/user/updateUser";
        }
    }

    @RequestMapping(value = "/deleteUser/{uid}")
    public String deleteUser(@PathVariable String uid){
        Integer integer=userService.deleteUser(Integer.parseInt(uid));

        return "jsp/user/user";
    }





}
